package com.ghn.cocknovel.ui.activity.work

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.example.basemodel.base.BaseActivity
import com.ghn.cocknovel.BR
import com.ghn.cocknovel.R
import com.ghn.cocknovel.databinding.ActivityWorkCalendarBinding
import com.ghn.cocknovel.ext.GONE
import com.ghn.cocknovel.ext.VISIBLE
import com.ghn.cocknovel.ui.activity.work.vm.WorkCalendarViewModel
import com.ghn.cocknovel.utils.MDateUtils
import com.kt.NetworkModel.utils.MLog
import java.text.SimpleDateFormat
import java.util.*
import kotlin.collections.HashMap

/**
 *@author: Administrator
 *2023/10/17
 *desc:
 */
class WorkCalendarActivity : BaseActivity<ActivityWorkCalendarBinding, WorkCalendarViewModel>() {

    var mTagerYearMonth = MDateUtils.getCurrentTime(MDateUtils.DATE_STYLE_6)
    var mTagerYearMonthDay = MDateUtils.getCurrentTime(MDateUtils.DATE_STYLE_4)
    val mWorkAdapter by lazy { WorkAdapter() }
    var mWorkDataMap: HashMap<String, String> = HashMap()
    var dateList = ArrayList<String>()
    var selectTime = ""
    override fun initVariableId(): Int = BR.mode

    override fun initContentView(savedInstanceState: Bundle?): Int = R.layout.activity_work_calendar
    override fun initParam() {
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
        window.setGravity(Gravity.BOTTOM)
        val split = mTagerYearMonth.split("-")
        binding.tvYearMont.text = split[0] + "/" + split[1]
        mWorkAdapter.bindToRecyclerView(binding.mList)
        binding.btnClose.setOnClickListener {
            finish()
        }
        binding.viewTopBg.setOnClickListener {
            finish()
        }
        binding.btnSubmit.setOnClickListener {
            if (selectTime.isEmpty()) {
                showMsg("请选择时间!")
                return@setOnClickListener
            }
            val intent = Intent()
            intent.putExtra("selectTime", selectTime)
            setResult(Activity.RESULT_OK, intent)
            finish()
        }
        binding.btnLeftMont.setOnClickListener {
            mTagerYearMonth = MDateUtils.getChangeMonth("yyyy-MM", mTagerYearMonth, -1)
            val split = mTagerYearMonth.split("-")
            binding.tvYearMont.setText(split[0] + "/" + split[1])
            initCalendarData()
        }
        binding.btnRightMont.setOnClickListener {
            mTagerYearMonth = MDateUtils.getChangeMonth("yyyy-MM", mTagerYearMonth, 1)
            val split = mTagerYearMonth.split("-")
            binding.tvYearMont.setText(split[0] + "/" + split[1])
            initCalendarData()
        }
        binding.btnToDayWork.setOnClickListener {
            mTagerYearMonthDay = MDateUtils.getCurrentTime(MDateUtils.DATE_STYLE_4)
            mTagerYearMonth = MDateUtils.getCurrentTime(MDateUtils.DATE_STYLE_6)
            val split = mTagerYearMonth.split("-")
            binding.tvYearMont.setText(split[0] + "/" + split[1])
            initCalendarData()
        }
        initCalendarData()
        viewModel.querySchedulePlanDateListNet()
        viewModel.mDateListLiveData.observe(this) {
            mWorkDataMap.clear()
            it.forEach {
                mWorkDataMap.put(it, "1")
            }
            mWorkAdapter.notifyDataSetChanged()
        }
    }


    private fun initCalendarData() {
        dateList.clear()
        val m = mTagerYearMonth.split("-")

        var ms = if (m[1].toInt() < 10) "0${m[1]}" else m[1].toString()

        val monthDays = getMonthDays()
        for (day in 1..monthDays) {
            if (day < 10) {
                dateList.add("${m[0]}-${ms}-0$day")
            } else {
                dateList.add("${m[0]}-${ms}-$day")
            }
        }
        //根据当前时间判断当前周几
        val calendar = Calendar.getInstance()
        calendar.time = SimpleDateFormat(MDateUtils.DATE_STYLE_4).parse(dateList.get(0))
        val DAY_OF_WEEK = calendar.get(Calendar.DAY_OF_WEEK) - 1
        for (d in 0 until DAY_OF_WEEK) {
            dateList.add(0, "")
        }
        mWorkAdapter.setNewData(dateList)
        mWorkAdapter.notifyDataSetChanged()
    }


    inner class WorkAdapter : BaseQuickAdapter<String, BaseViewHolder>, BaseQuickAdapter.OnItemClickListener {
        constructor() : super(R.layout.item_work_calendar, null) {
            onItemClickListener = this
        }

        override fun convert(helper: BaseViewHolder, item: String) {
            try {
                if (item.isEmpty()) {
                    helper.setText(R.id.tvDay, "")
                    helper.setBackgroundColor(R.id.clRoot, getColor(R.color.colorWhite))
                    helper.setTextColor(R.id.tvDay, getColor(R.color.colorBlack))
                    helper.getView<View>(R.id.tvDesc).GONE()
                    helper.getView<View>(R.id.tvDian).GONE()
                } else {
                    helper.setText(R.id.tvDay, SimpleDateFormat(MDateUtils.DATE_STYLE_8).format(SimpleDateFormat("yyyy-MM-dd").parse(item)))
                    helper.setBackgroundColor(R.id.clRoot, if (mTagerYearMonthDay.equals(item)) getColor(R.color.color_base_blue) else getColor(R.color.colorWhite))
                    helper.setTextColor(R.id.tvDay, if (mTagerYearMonthDay.equals(item)) getColor(R.color.colorWhite) else getColor(R.color.colorBlack))
                    //不等于 NULL  代表当前日期是当月的
                    val get = mWorkDataMap.get(item)
                    if (get != null) {
                        if (selectTime.isEmpty()) {
                            selectTime = item
                        }
                        helper.getView<View>(R.id.tvDesc).VISIBLE()
                        helper.getView<View>(R.id.tvDian).VISIBLE()
                    } else {
                        helper.getView<View>(R.id.tvDesc).GONE()
                        helper.getView<View>(R.id.tvDian).GONE()
                    }
                }

            } catch (e: Exception) {
                e.printStackTrace()
            }
        }

        override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View?, position: Int) {
            val data = adapter.getItem(position) as String
            if (data.isEmpty()) return
            mTagerYearMonthDay = data
            selectTime = data
            notifyDataSetChanged()
        }

    }


    /**
     * 一个月有多少天
     *
     * @param yearMonth
     * @return
     */
    fun getMonthDays(): Int {
        try {
            val calendar = Calendar.getInstance()
            calendar.time = SimpleDateFormat("yyyy-MM").parse(mTagerYearMonth)
            return calendar.getActualMaximum(Calendar.DAY_OF_MONTH)
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return 0
    }
}